package cn.cjlmonster.hyouka_assistant

import cn.cjlmonster.hyouka_assistant.audio.AudioRecorder
import cn.cjlmonster.hyouka_assistant.webView.NativeWebViewFactory
import cn.cjlmonster.hyouka_assistant.webView.WebViewPool
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding

/**
 * 注册插件
 */
class HyoukaAssistantPlugin : FlutterPlugin, ActivityAware {

    override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        WebViewPool.getInstance().init(flutterPluginBinding.applicationContext)
        NativeWebViewFactory.registerWith(flutterPluginBinding)
        WKPlugin.bindMethodChannel(flutterPluginBinding)
        AudioRecorder.bindEventChannel(flutterPluginBinding)
    }

    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        WebViewPool.getInstance().deInit()
        WKPlugin.unbindMethodChannel()
        AudioRecorder.unbindEventChannel()
    }

    override fun onAttachedToActivity(binding: ActivityPluginBinding) {
        WKPlugin.bindActivity(binding.activity)
    }

    override fun onDetachedFromActivity() {
        WKPlugin.unbindActivity()
    }

    override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
        onAttachedToActivity(binding)
    }

    override fun onDetachedFromActivityForConfigChanges() {
        onDetachedFromActivity()
    }
}